package com.example.lettuce;

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;
import java.util.function.Function;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/6/1 19:57
 * @Description TODO
 */
@Slf4j
public class LettuceTest {
    public static void main(String[] args) {
        String command = LettuceHandle.command(syncCommands -> syncCommands.get("test"));
        log.info("result:{}",command);
    }
}

@Slf4j
class LettuceHandle {

    static  <R>  R command(Function<RedisCommands<String, String>,R> function) {
        RedisClient redisClient = RedisClient.create("redis://localhost:6379"); // change to reflect your environment
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisCommands<String, String> syncCommands = connection.sync();
        try {
           return function.apply(syncCommands);
        } catch (Exception e) {
            log.error("liyuhang: 捕捉到异常 {}  异常栈信息:",e.getMessage(),e);
        } finally {
            connection.close();
            redisClient.shutdown();
        }
        return null;
    }

}
